package com.sailing.core.demo.sync;

public class SynchronizedTest {
	private Object obj = new Object();
	/**
	 * 对象锁
	 */
	public void test1() {
		synchronized (this) {
			int i = 50;
			while (i-- > 0) {
				System.out.println(Thread.currentThread().getName() + " : " + i);
				try {
					Thread.sleep(500);
				} catch (InterruptedException ie) {
				}
			}
		}
	}

	/**
	 * 类锁
	 */
	public static synchronized void test2() {
		int i = 50;
		while (i-- > 0) {
			System.out.println(Thread.currentThread().getName() + " : " + i);
			try {
				Thread.sleep(500);
			} catch (InterruptedException ie) {
			}
		}
	}

	/**
	 * 私有锁
	 */
	public void test3() {
		synchronized (obj) {
			int i = 50;
			while (i-- > 0) {
				System.out.println(Thread.currentThread().getName() + " : " + i);
				try {
					Thread.sleep(500);
				} catch (InterruptedException ie) {
				}
			}
		}
	}
}
